package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.domain.dao.ClueStageDao;
import com.xbongbong.saas.domain.entity.ClueStageEntity;
import com.xbongbong.saas.model.ClueStageModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long
 * @version v1.0
 * @date 2020/11/6 14:34
 * @since v1.0
 */
@Service
public class ClueStageModelImpl implements ClueStageModel {

    @Resource
    private ClueStageDao clueStageDao;

    @Override
    public Integer insert(ClueStageEntity entity) throws XbbException {
        entity.setAddTime((long) DateUtil.getInt());
        entity.setUpdateTime((long) DateUtil.getInt());
        clueStageDao.insert(entity);
        return entity.getId().intValue();
    }

    @Override
    public Integer update(ClueStageEntity entity) throws XbbException {
        entity.setUpdateTime((long) DateUtil.getInt());
        clueStageDao.update(entity);
        return entity.getId().intValue();
    }

    @Override
    public List<ClueStageEntity> findEntitys(Map<String, Object> param) {
        return clueStageDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return clueStageDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public ClueStageEntity getByKey(Long key, String corpid) {
        return clueStageDao.getByKey(key,corpid);
    }

    @Override
    public ClueStageEntity getByCode(String corpid, Long formId, String code) {
        return clueStageDao.getByCode(corpid, formId, code);
    }

    @Override
    public Long save(ClueStageEntity ClueStageEntity) throws XbbException {
        if (ClueStageEntity.getId() != null){
            update(ClueStageEntity);
        }else {
            insert(ClueStageEntity);
        }
        return ClueStageEntity.getId();
    }

    @Override
    public List<ClueStageEntity> getDefaultList() {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid","0");
        map.put("del",0);
        return findEntitys(map);
    }

    @Override
    public void insertBatch(List<ClueStageEntity> list) {
        clueStageDao.insertBatch(list);
    }

    @Override
    public List<ClueStageEntity> getList(String corpid,Long formId, Integer enable) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("formId",formId);
        if (Objects.nonNull(enable)) {
            map.put("enable", enable);
        }
        map.put("del",0);
        map.put("orderByStr","sort desc");
        return findEntitys(map);
    }

    @Override
    public void updateBatch(List<ClueStageEntity> clueStageEntityList, String corpid) {
        clueStageDao.updateBatch(clueStageEntityList, corpid);
    }

    @Override
    public void updateBatchDefaultAndLinkOption(List<ClueStageEntity> clueStageEntityList, String corpid) {
        clueStageDao.updateBatchDefaultAndLinkOption(clueStageEntityList, corpid);
    }

    @Override
    public void updateBatch4MirrorRollback(List<ClueStageEntity> clueStageEntityList, String corpid) {
        clueStageDao.updateBatch4MirrorRollback(clueStageEntityList, corpid);
    }

    @Override
    public void updateBatch4Script(List<ClueStageEntity> clueStageEntityList) {
        if(CollectionsUtil.isNotEmpty(clueStageEntityList)) {
            int size = clueStageEntityList.size();
            int page = (size - 1) / 1000;
            for (int currPage = 0; currPage <= page; currPage++) {
                int offset = currPage * 1000;
                clueStageDao.updateBatch4Script(clueStageEntityList.subList(offset, (size - offset < 1000) ? size : offset + 1000));
            }
        }
    }

    @Override
    public void insertBatch4Script(List<ClueStageEntity> list) {
        clueStageDao.insertBatch4Script(list);
    }

    @Override
    public Integer insert4Script(ClueStageEntity entity) {
        return clueStageDao.insert4Script(entity);
    }

    @Override
    public Integer deleteBatch(List<PaasStageEntity> clueStageEntityList, String corpid) {
        return clueStageDao.deleteBatch(clueStageEntityList, corpid);
    }
}
